package com.homework.ch08;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class Test1 {
    public static void main(String[] args) {
        Collection<Integer> co1=new ArrayList<Integer>();
        co1.add(1);
        co1.add(2);
        co1.add(3);
        co1.add(4);
        co1.add(5);
        //1.转换成数组for循环遍历
        for (int i = 0; i < co1.size(); i++) {
            System.out.print(co1.toArray()[i]+" ");
        }
        System.out.println();
        System.out.println("------------------");
        Object[] arr=co1.toArray();
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+" ");
        }
        System.out.println();
        System.out.println("------------------");
        //2.使用foreach
        for(Integer obj:co1)
        {
            System.out.print(obj+" ");
        }
        System.out.println();
        System.out.println("------------------");
        //3.使用迭代器，iterator
        Iterator<Integer> it=co1.iterator();
        while(it.hasNext())
        {
            int num=it.next();
            System.out.print(num+" ");
        }
        System.out.println();
        System.out.println("------------------");
        //4.使用流stream
        co1.forEach(System.out::print);
        System.out.println();
        co1.forEach((obj)-> System.out.print(obj+" "));
    }
}
